package com.dongliu.service.impl;

import com.dongliu.dao.BookDao;
import com.dongliu.dao.LentRecordDao;
import com.dongliu.dao.UserDao;
import com.dongliu.domain.Book;
import com.dongliu.domain.LentRecord;
import com.dongliu.domain.User;
import com.dongliu.service.LentRecordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class LentRecordServiceImpl implements LentRecordService {

    @Autowired
    private LentRecordDao lentRecordDao;
    @Autowired
    private BookDao bookDao;
    @Autowired
    private UserDao userDao;

    public boolean save(LentRecord lentRecord) {
        return lentRecordDao.save(lentRecord) > 0;
    }

    public boolean update(LentRecord lentRecord) {
        return lentRecordDao.update(lentRecord) > 0;
    }

    public boolean delete(Integer lentId) {
        return lentRecordDao.delete(lentId) > 0;
    }

    public List<LentRecord> getByUsername(String username) {
        return lentRecordDao.getByUsername(username);
    }

    public LentRecord getByLentId(Integer lentId){
        return lentRecordDao.getByLentId(lentId);
    }

    public List<LentRecord> getAll() {
        return lentRecordDao.getAll();
    }

    public int getCount(){
        return lentRecordDao.getCount();
    }

    public boolean createRecord(Book book, User user){
        try {
            LentRecord lentRecord = new LentRecord();
            int lentNumber = book.getLentNumber();
            lentNumber++;
            book.setLentNumber(lentNumber);
            bookDao.update(book);
            int lentNumber2 = user.getLentNumber();
            lentNumber2++;
            user.setLentNumber(lentNumber2);
            userDao.updateById(user);
            lentRecord.setBookId(book.getId());
            lentRecord.setBookName(book.getName());
            lentRecord.setUsername(user.getUsername());
            lentRecord.setName(user.getName());
            lentRecord.setLentDate(new Date());
            return lentRecordDao.save(lentRecord) > 0;
        }catch (Exception e){
            System.out.println("创建借书记录出错！");
        }
        return false;
    }

    public List<LentRecord> getAllByUsername(String username) {
        return lentRecordDao.getAllByUsername(username);
    }

    public List<LentRecord> getNotReturnByUsername(String username) {
        return lentRecordDao.getNotReturnByUsername(username);
    }

    public LentRecord returnBook(LentRecord lentRecord) {
        Date date = new Date();
        // 还书要扣钱
        int days = (int)Math.ceil(((date.getTime() - lentRecord.getLentDate().getTime()) / (1.0*1000*3600*24)));
        lentRecord.setReturnDate(date);
        User user = userDao.getByUsername(lentRecord.getUsername());
        float bill = (float) (0.1*days);
        float money = (float) (user.getMoney()-bill);
        user.setMoney(money);
        // 已借数量--
        user.setLentNumber(user.getLentNumber()-1);
        userDao.update(user);
        lentRecord.setBill(bill);
        lentRecordDao.update(lentRecord);
        // 书已借数量--
        Book book = bookDao.getById(lentRecord.getBookId());
        book.setLentNumber(book.getLentNumber()-1);
        bookDao.update(book);
        boolean flag =  lentRecordDao.update(lentRecord) > 0;
        return flag ? lentRecord : null;
    }
}
